package GUI;

import ModelLayer.*;
import CtrlLayer.*;

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ContractorsGUI extends JFrame implements ActionListener{
    private Container container;
    private JPanel panelContractors;
    private JPanel panelCreateContractor;
    private JPanel panelFindContractor;
    private JPanel panelFindContractor2;
    private JPanel panelUpdateContractor;
    private JPanel panelUpdateContractor2;
    private JPanel panelDeleteContractor;
    private JPanel panelDeleteContractor2;
    private JButton back;
    private ContractorController contractorController;
    private int sessionId;

    public ContractorsGUI(int sessionId){
        panelContractors = new JPanel(new GridBagLayout());
        panelCreateContractor = new JPanel(new GridBagLayout());
        panelFindContractor = new JPanel(new GridBagLayout());
        panelFindContractor2 = new JPanel(new GridBagLayout());
        panelUpdateContractor = new JPanel(new GridBagLayout());
        panelUpdateContractor2 = new JPanel(new GridBagLayout());
        panelDeleteContractor = new JPanel(new GridBagLayout());
        panelDeleteContractor2 = new JPanel(new GridBagLayout());
        contractorController = new ContractorController();
        this.sessionId = sessionId;

        this.setTitle("Vestbjerg company system");
        this.setLocation(300,50);
        this.setVisible(true);
        this.setSize(400, 470);
        this.setResizable(false);
        contractorsMainMenu();
    }

    public void actionPerformed(ActionEvent e){

    }

    public void makeContractorsMenu(){
        GridBagConstraints gbc = new GridBagConstraints();

        JButton addContractor = new JButton("Add Contractor");
        addContractor.setPreferredSize(new Dimension(150, 25));
        addContractor.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    createContractor();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelContractors.add(addContractor, gbc);

        JButton showContractors = new JButton("Show Contractor");
        showContractors.setPreferredSize(new Dimension(150, 25));
        showContractors.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findContractor();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelContractors.add(showContractors, gbc);

        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2){
            JButton deleteContractor = new JButton("Delete Contractor");
            deleteContractor.setPreferredSize(new Dimension(150, 25));
            deleteContractor.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){
                        container = getContentPane();
                        container.removeAll();
                        deleteContractorFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                    }
                }
            );
            gbc.gridx = 0;
            gbc.gridy = 4;
            gbc.insets = new Insets(5,5,5,5);
            panelContractors.add(deleteContractor, gbc);

            JButton updateContractor = new JButton("Update Contractor");
            updateContractor.setPreferredSize(new Dimension(150, 25));
            updateContractor.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){
                        container = getContentPane();
                        container.removeAll();
                        updateContractorFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                    }
                }
            );
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.insets = new Insets(5,5,5,5);
            panelContractors.add(updateContractor, gbc);
        }

        JButton back = new JButton("Close");
        back.setPreferredSize(new Dimension(150, 25));
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    dispose();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelContractors.add(back, gbc);
    }

    public void contractorsMainMenu()
    {
        makeContractorsMenu();
        this.getContentPane().add(panelContractors, BorderLayout.NORTH);
    }

    private void create()
    {
        JTextField textId = new JTextField();
        JTextField textName = new JTextField();
        JTextField textEmail = new JTextField();
        JTextField textPhoneNumber = new JTextField();
        JTextField textBankAccount = new JTextField();
        JLabel labelHeader;
        JLabel labelId;
        JLabel labelName;
        JLabel labelEmail;
        JLabel labelPhoneNumber;
        JLabel labelBankAccount;
        JLabel errorMessage;
        JButton create;
        JButton clear;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");
        textName.setText("");
        textEmail.setText("");
        textPhoneNumber.setText("");
        textBankAccount.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateContractor.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelCreateContractor.add(textId,gbc);

        labelName = new JLabel("Name:", JLabel.RIGHT);
        labelName.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateContractor.add(labelName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textName.setPreferredSize(new Dimension(100,25));
        panelCreateContractor.add(textName,gbc);

        labelEmail = new JLabel("E-Mail:", JLabel.RIGHT);
        labelEmail.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateContractor.add(labelEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textEmail.setPreferredSize(new Dimension(100,25));
        panelCreateContractor.add(textEmail,gbc);

        labelPhoneNumber = new JLabel("Phone number:", JLabel.RIGHT);
        labelPhoneNumber.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateContractor.add(labelPhoneNumber, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhoneNumber.setPreferredSize(new Dimension(100,25));
        panelCreateContractor.add(textPhoneNumber,gbc);

        labelBankAccount = new JLabel("Bank account:", JLabel.RIGHT);
        labelBankAccount.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateContractor.add(labelBankAccount, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textBankAccount.setPreferredSize(new Dimension(100,25));
        panelCreateContractor.add(textBankAccount,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateContractor.add(errorMessage, gbc);

        create = new JButton("Create");
        create.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        contractorController.createContractor(Integer.parseInt(textId.getText()), 
                            textName.getText(), 
                            textEmail.getText(), 
                            textPhoneNumber.getText(), 
                            textBankAccount.getText());

                        container = getContentPane();
                        panelCreateContractor.removeAll();
                        container.removeAll();
                        createContractor();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        textId.setText("");
                        textName.setText("");
                        textEmail.setText("");
                        textPhoneNumber.setText("");
                        textBankAccount.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex)
                    {
                        container = getContentPane();
                        container.removeAll();
                        createContractor();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        textName.setText("");
                        textEmail.setText("");
                        textPhoneNumber.setText("");
                        textBankAccount.setText("");
                        errorMessage.setText("");
                        errorMessage.setText("Not possible!");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        create.setPreferredSize(new Dimension(100,25));
        panelCreateContractor.add(create,gbc);

        clear = new JButton("Clear");
        clear.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    panelCreateContractor.removeAll();
                    container.removeAll();
                    createContractor();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    textId.setText("");
                    textName.setText("");
                    textEmail.setText("");
                    textPhoneNumber.setText("");
                    textBankAccount.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        clear.setPreferredSize(new Dimension(100,25));
        panelCreateContractor.add(clear,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    textId.setText("");
                    textName.setText("");
                    textEmail.setText("");
                    textPhoneNumber.setText("");
                    textBankAccount.setText("");
                    errorMessage.setText("");
                    container = getContentPane();
                    panelCreateContractor.removeAll();
                    container.removeAll();
                    contractorsMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelCreateContractor.add(back,gbc);
    }

    public void createContractor()
    {
        create();
        this.getContentPane().add(panelCreateContractor, BorderLayout.NORTH);
    }

    private void find()
    {
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelFindContractor.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelFindContractor.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelFindContractor.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        findContractorSecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex)
                    {
                        container = getContentPane();
                        container.removeAll();
                        findContractor();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("No Contractor!");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelFindContractor.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    textId.setText("");
                    errorMessage.setText("");
                    container = getContentPane();
                    panelFindContractor.removeAll();
                    container.removeAll();
                    contractorsMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelFindContractor.add(back,gbc);
    }

    public void findContractor()
    {
        find();
        this.getContentPane().add(panelFindContractor, BorderLayout.NORTH);
    }

    public void findSecondWindow(int id)
    {
        JTextField textId = new JTextField();
        JTextField textName = new JTextField();
        JTextField textEmail = new JTextField();
        JTextField textPhoneNumber = new JTextField();
        JTextField textBankAccount = new JTextField();
        JLabel labelId;
        JLabel labelName;
        JLabel labelEmail;
        JLabel labelPhoneNumber;
        JLabel labelBankAccount;
        JButton ok;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setEditable(false);
        textName.setEditable(false);
        textEmail.setEditable(false);
        textPhoneNumber.setEditable(false);
        textBankAccount.setEditable(false);

        textId.setText(Integer.toString(id));
        textName.setText(contractorController.findContractor(id).getName());
        textEmail.setText(contractorController.findContractor(id).getEmail());
        textPhoneNumber.setText(contractorController.findContractor(id).getPhoneNumber());
        textBankAccount.setText(contractorController.findContractor(id).getBankAccount());

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelFindContractor2.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelFindContractor2.add(textId,gbc);

        labelName = new JLabel("Name:", JLabel.RIGHT);
        labelName.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelFindContractor2.add(labelName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textName.setPreferredSize(new Dimension(100,25));
        panelFindContractor2.add(textName,gbc);

        labelEmail = new JLabel("E-Mail:", JLabel.RIGHT);
        labelEmail.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelFindContractor2.add(labelEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textEmail.setPreferredSize(new Dimension(100,25));
        panelFindContractor2.add(textEmail,gbc);

        labelPhoneNumber = new JLabel("Phone number:", JLabel.RIGHT);
        labelPhoneNumber.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelFindContractor2.add(labelPhoneNumber, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhoneNumber.setPreferredSize(new Dimension(100,25));
        panelFindContractor2.add(textPhoneNumber,gbc);

        labelBankAccount = new JLabel("Bank Account:", JLabel.RIGHT);
        labelBankAccount.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelFindContractor2.add(labelBankAccount, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textBankAccount.setPreferredSize(new Dimension(100,25));
        panelFindContractor2.add(textBankAccount,gbc);

        ok = new JButton("OK");
        ok.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findContractor();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        ok.setPreferredSize(new Dimension(100,25));
        panelFindContractor2.add(ok,gbc);
    }

    public void findContractorSecondWindow(int id)
    {
        findSecondWindow(id);
        this.getContentPane().add(panelFindContractor2, BorderLayout.NORTH);
    }

    private void updateFirstWindow()
    {
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateContractor.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelUpdateContractor.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateContractor.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        updateContractorSecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex)
                    {
                        container = getContentPane();
                        container.removeAll();
                        updateContractorFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("No Contractor!");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelUpdateContractor.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    contractorsMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    textId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelUpdateContractor.add(back,gbc);
    }

    private void updateSecondWindow(int id)
    {
        JTextField textId = new JTextField();
        JTextField textName = new JTextField();
        JTextField textEmail = new JTextField();
        JTextField textPhoneNumber = new JTextField();
        JTextField textBankAccount = new JTextField();
        JLabel labelId;
        JLabel labelName;
        JLabel labelEmail;
        JLabel labelPhoneNumber;
        JLabel labelBankAccount;
        JButton update;
        JButton cancel;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setEditable(false);
        textId.setText(Integer.toString(id));
        textName.setText(contractorController.findContractor(id).getName());
        textEmail.setText(contractorController.findContractor(id).getEmail());
        textPhoneNumber.setText(contractorController.findContractor(id).getPhoneNumber());
        textBankAccount.setText(contractorController.findContractor(id).getBankAccount());

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateContractor2.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelUpdateContractor2.add(textId,gbc);

        labelName = new JLabel("Name:", JLabel.RIGHT);
        labelName.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateContractor2.add(labelName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textName.setPreferredSize(new Dimension(100,25));
        panelUpdateContractor2.add(textName,gbc);

        labelEmail = new JLabel("E-Mail:", JLabel.RIGHT);
        labelEmail.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateContractor2.add(labelEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textEmail.setPreferredSize(new Dimension(100,25));
        panelUpdateContractor2.add(textEmail,gbc);

        labelPhoneNumber = new JLabel("Phone number:", JLabel.RIGHT);
        labelPhoneNumber.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateContractor2.add(labelPhoneNumber, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhoneNumber.setPreferredSize(new Dimension(100,25));
        panelUpdateContractor2.add(textPhoneNumber,gbc);

        labelBankAccount = new JLabel("BankAccount", JLabel.RIGHT);
        labelBankAccount.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateContractor2.add(labelBankAccount, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textBankAccount.setPreferredSize(new Dimension(100,25));
        panelUpdateContractor2.add(textBankAccount,gbc);

        update = new JButton("Update");
        update.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    contractorController.updateContractor(id, textName.getText(), 
                        textEmail.getText(), 
                        textPhoneNumber.getText(), 
                        textBankAccount.getText());

                    container = getContentPane();
                    container.removeAll();
                    updateContractorFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        update.setPreferredSize(new Dimension(100,25));
        panelUpdateContractor2.add(update,gbc);

        cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    updateContractorFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        cancel.setPreferredSize(new Dimension(100,25));
        panelUpdateContractor2.add(cancel,gbc);

    }

    public void updateContractorFirstWindow()
    {
        updateFirstWindow();
        this.getContentPane().add(panelUpdateContractor, BorderLayout.NORTH);
    }

    public void updateContractorSecondWindow(int id)
    {
        updateSecondWindow(id);
        this.getContentPane().add(panelUpdateContractor2, BorderLayout.NORTH);
    }

    private void deleteFirstWindow()
    {
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteContractor.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelDeleteContractor.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteContractor.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        deleteContractorSecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex)
                    {
                        container = getContentPane();
                        container.removeAll();
                        deleteContractorFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("No Contractor!");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelDeleteContractor.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    contractorsMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    textId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelDeleteContractor.add(back,gbc);
    }

    private void deleteSecondWindow(int id)
    {
        JTextField textId = new JTextField();
        JTextField textName = new JTextField();
        JTextField textEmail = new JTextField();
        JTextField textPhoneNumber = new JTextField();
        JTextField textBankAccount = new JTextField();
        JLabel labelId;
        JLabel labelName;
        JLabel labelEmail;
        JLabel labelPhoneNumber;
        JLabel labelBankAccount;
        JButton delete;
        JButton cancel;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setEditable(false);
        textName.setEditable(false);
        textEmail.setEditable(false);
        textPhoneNumber.setEditable(false);
        textBankAccount.setEditable(false);

        textId.setText(Integer.toString(id));
        textName.setText(contractorController.findContractor(id).getName());
        textEmail.setText(contractorController.findContractor(id).getEmail());
        textPhoneNumber.setText(contractorController.findContractor(id).getPhoneNumber());
        textBankAccount.setText(contractorController.findContractor(id).getBankAccount());

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteContractor2.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelDeleteContractor2.add(textId,gbc);

        labelName = new JLabel("Name:", JLabel.RIGHT);
        labelName.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteContractor2.add(labelName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textName.setPreferredSize(new Dimension(100,25));
        panelDeleteContractor2.add(textName,gbc);

        labelEmail = new JLabel("E-Mail:", JLabel.RIGHT);
        labelEmail.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteContractor2.add(labelEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textEmail.setPreferredSize(new Dimension(100,25));
        panelDeleteContractor2.add(textEmail,gbc);

        labelPhoneNumber = new JLabel("Phone number:", JLabel.RIGHT);
        labelPhoneNumber.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteContractor2.add(labelPhoneNumber, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhoneNumber.setPreferredSize(new Dimension(100,25));
        panelDeleteContractor2.add(textPhoneNumber,gbc);

        labelBankAccount = new JLabel("Bank Account:", JLabel.RIGHT);
        labelBankAccount.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteContractor2.add(labelBankAccount, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textBankAccount.setPreferredSize(new Dimension(100,25));
        panelDeleteContractor2.add(textBankAccount,gbc);

        delete = new JButton("Delete");
        delete.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    contractorController.deleteContractor(id);
                    container = getContentPane();
                    container.removeAll();
                    deleteContractorFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        delete.setPreferredSize(new Dimension(100,25));
        panelDeleteContractor2.add(delete,gbc);

        cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    deleteContractorFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        cancel.setPreferredSize(new Dimension(100,25));
        panelDeleteContractor2.add(cancel, gbc);
    }

    private void deleteContractorFirstWindow()
    {
        deleteFirstWindow();
        this.getContentPane().add(panelDeleteContractor, BorderLayout.NORTH);
    }

    private void deleteContractorSecondWindow(int id)
    {
        deleteSecondWindow(id);
        this.getContentPane().add(panelDeleteContractor2, BorderLayout.NORTH);
    }
}